#include "ysip.hpp"

void out_region_info(REGION_INFO * region_info)
{
	if(region_info != NULL)
	{
		cout << " Region Info is " <<endl;
		cout << "  Region id is: " << region_info->id <<endl;
		cout << "  Region parent_id is : " << region_info->parent_id <<endl;
		cout << "  Region name is : " << region_info->name << endl;
		cout << "  Region level is : " << region_info->level << endl;
	}
	else
	{
		cout << " Region info is empty !" << endl;
	}
}

void out_region_info_x(REGION_INFO_X * region_info_x, int depth = 1)
{
	if(region_info_x != NULL)
	{
		cout << "Region Info X is : " << endl;
		out_region_info(&region_info_x->info);
		for(int i = 0, len = region_info_x->length; i < len ; i++)
		{
			out_region_info_x(region_info_x->childs[i]);
		}
	}
	else
	{
		cout << " Region Info X is empty! " << endl;
	}
}
int test_load(int second = 10)
{
	int pass = 0;
	int succ = 0;
	int fail = 0;
	bool ret = false;
	uint begin = 0, end = 0;
	REGION_INFO region_info;
	
	int j = 0;
	const char *IPS[] = {
		"203.209.241.97",
		"119.42.240.115",
		"119.42.240.139",
		"71.5.7.191",
		"220.181.38.84",
		"202.108.22.46",
		"119.147.15.13",
		"58.61.166.142"};
	string ip;

	begin = time(NULL);
	for(succ = 0, fail = 0, pass = 0; pass < second ; ) {
		if((succ + fail) % 10000 == 0) {
			pass = time(NULL) - begin;
		}
		ip = IPS[j];
		ret = get_ip_info(ip, & region_info);
		if(ret) {
			succ ++;
		} else {
			fail ++;
		}
		j = j >= 8 ? 0 : j ;
	}
	end = time(NULL);

	cout << "Begin is: " << begin << endl;
	cout << "End is: " << end << endl;
	cout << "Succ : " << succ << endl;
	cout << "Failed : " << fail <<endl;
	cout << "End - Begin is " <<  end - begin << endl;
	cout << "Avg is " <<  succ / (end - begin) << endl;
}

void test_get_ip_info(string ip) 
{
	bool ret = false;
	REGION_INFO region_info;
	ret = get_ip_info(ip, & region_info);
	if(ret) {
		cout << "IP is " << ip << endl;
		out_region_info(&region_info);
	} else {
		cerr << "Ip is " << ip << endl;
		cerr << "Cann't find this ip in the db" << endl;
	}
}

void test_check_index()
{

}
void test_get_stat()
{

}
void test_get_all_regions()
{
	int len = 0;
	int i = 0;
	REGION_INFO ** region_infos;
	region_infos = get_all_regions(len);
	if(region_infos != NULL)
	{
		for( i = 0 ; i < len ; i ++)
		{
			out_region_info(region_infos[i]);
		}
		free_region_info(region_infos, len);
	}
	else
	{
		cerr << "Get all error!" <<endl;
	}
}
void test_get_region_info(int region_id)
{
	REGION_INFO region_info;
	cout << " The region id is " << region_id << endl;
	if(get_region_info(region_id, &region_info))
	{
		out_region_info(&region_info);	
	}
	else
	{
		cout << " Cann't find the region id info " << endl;
	}
}


void test_get_region_info_by_en_name(string region)
{
	REGION_INFO region_info;
	cout << " The region name is '" << region << "'" << endl;
	if(get_region_info_by_en_name(region.c_str(), &region_info))
	{
		out_region_info(&region_info);	
	}
	else
	{
		cout << " Cann't find the region name " << endl;
	}
}


void test_get_region_info_by_name(string region)
{
	REGION_INFO region_info;
	cout << " The region name is '" << region << "'" << endl;
	if(get_region_info_by_name(region.c_str(), &region_info))
	{
		out_region_info(&region_info);	
	}
	else
	{
		cout << " Cann't find the region name " << endl;
	}
}


void test_get_region_info_by_name_ex(string region)
{
	int len = 0;
	REGION_INFO ** region_infos;
	cout << " The region name is '" << region << "'" << endl;
	region_infos = get_region_info_by_name_ex(region.c_str(), len);
	if(region_infos != NULL)
	{
		for( int i = 0; i < len ; i ++) {
			out_region_info(region_infos[i]);	
		}
		free_region_info(region_infos, len);
	}
	else
	{
		cout << " Cann't find the region name " << endl;
	}
}

void test_get_region_parents(int region_id)
{
	int len = 0;
	REGION_INFO ** region_parents;
	region_parents = get_region_parents(region_id, len);
	if(region_parents != NULL)
	{
		for( int i = 0 ; i < len ; i++)
		{
			out_region_info(region_parents[i]);
		}
		free_region_info(region_parents, len);
	}
	else
	{
		cout << " Cann't find the region " << optarg << " 's parents" <<endl;
	}
				
}

void test_get_region_childs(int region_id)
{
	int len = 0;
	REGION_INFO_X * region_info_x;

	region_info_x = get_region_childs(region_id, 3);
	if(region_info_x != NULL)
	{
		out_region_info_x(region_info_x, 1);
		free_region_info_x(region_info_x);
	}
	else
	{
		cout << " Cann't find the region " << optarg << " 's childs" <<endl;
	}
}
void usage() {
 	cout << " YSIP v0.1.0   	 Nov 13, 2008      http://onemouse.cn/" << endl;
	cout << " Usage: ysip -c | -i <ip> | -l <second> | -v | -s | -a" << endl; 
	cout << "        ysip -r <region id> | -n <region name> | -p <region id>| -d <region id> "<<endl;
	cout << "        ysip -e <region en name> -f <region name>" <<endl;
	cout << "  -c read ip data from /home/y/var/ysip/ipdata.utf8 to db" << endl;
	cout << "  -i <ip> query the ip's region info" << endl;
	cout << "  -l <second> load test " << endl;
	cout << "  -v validate the index " << endl;
	cout << "  -s get the index stat " << endl;
	cout << "  -a list all the regions " << endl;
	cout << "  -r <region id> specific the region id you want to get " <<endl;
	cout << "  -n <region name> specific the region name you want to get " <<endl;
	cout << "  -f <region name> specific the region name you want to get, but fuzzy search " <<endl;
	cout << "  -e <region en name> specific the region en name you want to get " <<endl;
	cout << "  -p <region id> get the region id 's parents" << endl;
	cout << "  -d <region id> get the region id 's childs" << endl;
	exit(0);
}

int main(int argc, char ** argv) {
	int ch;
	bool flag = false;
	get_instance();
	while(( ch = getopt(argc, argv, "h?ci:l:vsr:n:p:d:ae:f:")) > -1) {
		flag = true;
		switch(ch) {
			case 'i':
				test_get_ip_info(optarg);
				break;
			case 'c':
				init_db();
				break;
			case 'l':
				test_load(atoi(optarg));
				break;
			case 'v':
				test_check_index();
				break;
			case 's':
				test_get_stat();
				break;
			case 'r':
				test_get_region_info(atoi(optarg));
				break;
			case 'n':
				test_get_region_info_by_name(optarg);
				break;
			case 'p':
				test_get_region_parents(atoi(optarg));
				break;
			case 'd':
				//for(int i = 0 ; i< 1000; i ++) {
					test_get_region_childs(atoi(optarg));
				//	if(i % 100 == 0) {
				//		cerr << i << endl;
				//	}
				//}
				break;
			case 'a':
				test_get_all_regions();
				break;
			case 'e':
				test_get_region_info_by_en_name(optarg);
				break;
			case 'f':
				test_get_region_info_by_name_ex(optarg);
				break;
			case 'h':
			default:
				usage();
				break;
		}
	}
	release_instance();
	if(!flag) {
		usage();
	}
	return 0;
}
